package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.ProductionDetail;
import com.geek.factory.result.Result;
import com.geek.factory.service.IProductionDetailService;
import com.geek.factory.vo.ProductionDetailQueryVO;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/production/details")
@Slf4j
public class ProductionDetailController {
    @Autowired
    private IProductionDetailService productionDetailService;

    @GetMapping("/plan/{planId}")
    public Result getByPlanId(@PathVariable Integer planId) {
        log.info("根据生产计划ID查询明细, planId: {}", planId);
        List<ProductionDetail> list = productionDetailService.lambdaQuery()
                .eq(ProductionDetail::getPlanId, planId)
                .list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }

    /**
     * 新增生产明细
     */
    @PostMapping
    public Result addDetail(@RequestBody ProductionDetail productionDetail) {
        log.info("新增生产明细, 参数: {}", productionDetail);
        productionDetailService.save(productionDetail);
        return new Result(SuccessConstant.SUCCESS, "新增成功");
    }

    /**
     * 根据ID查询生产明细
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询生产明细, id: {}", id);
        ProductionDetail detail = productionDetailService.getById(id);
        return new Result(SuccessConstant.SUCCESS, "查询成功", detail);
    }

    /**
     * 修改生产明细
     */
    @PutMapping
    public Result updateDetail(@RequestBody ProductionDetail productionDetail) {
        log.info("修改生产明细, 参数: {}", productionDetail);
        productionDetailService.updateById(productionDetail);
        return new Result(SuccessConstant.SUCCESS, "修改成功");
    }

    /**
     * 根据ID删除生产明细
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除生产明细, id: {}", id);
        productionDetailService.removeById(id);
        return new Result(SuccessConstant.SUCCESS, "删除成功");
    }


    @PostMapping("/list")
    public Result findPage(
            @ApiParam(value = "Current page number", required = true) @RequestParam(defaultValue = "1") Long now,
            @ApiParam(value = "Page size", required = true) @RequestParam(defaultValue = "5") Long size,
            @ApiParam(value = "Query criteria") @RequestBody ProductionDetailQueryVO query) {

        log.info("Received request - now: {}, size: {}, query: {}", now, size, query);
        Page<ProductionDetail> page = new Page<>(now, size);
        LambdaQueryWrapper<ProductionDetail> wrapper = new LambdaQueryWrapper<>();

        // Add query conditions based on the ProductionDetailQueryVO
        if (query != null) {
            if (query.getPlanId() != null) {
                wrapper.eq(ProductionDetail::getPlanId, query.getPlanId());
            }
            if (!StringUtils.isEmpty(query.getProcessName())) {
                wrapper.like(ProductionDetail::getProcessName, query.getProcessName()); // Assuming a productName field
            }
        }

        Page<ProductionDetail> resultPage = productionDetailService.page(page, wrapper);
        return new Result(SuccessConstant.SUCCESS, "查询成功", resultPage);
    }




}
